const jwt = require("jsonwebtoken");
const { APP_JWT_SECRET } = require("../config/config.default");
const authMiddleware = async (ctx, next) => {
    const token = ctx.headers.authorization;
    if (!token) {
        ctx.status = 401;
        ctx.body = { message: "Token is missing" };
        return;
    }

    try {
        const decoded = jwt.verify(token, APP_JWT_SECRET);
        ctx.state.user = decoded; // 将解码后的用户信息存储到 ctx.state 中
        await next();
    } catch (error) {
        ctx.status = 401;
        ctx.body = { message: "Invalid token", error };
    }
};

module.exports = authMiddleware;
